package com.info.zhiduoduo.core.service.wxmessage;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.info.zhiduoduo.common.configs.CommonConfig;
import com.info.zhiduoduo.common.configs.NotificationConfig;
import com.info.zhiduoduo.common.configs.PayConfig;
import com.info.zhiduoduo.common.configs.WhetherTestConfig;
import com.info.zhiduoduo.common.constants.Constants;
import com.info.zhiduoduo.common.constants.email.EmailType;
import com.info.zhiduoduo.common.utils.ContextUtil;
import com.info.zhiduoduo.common.utils.DateUtil;
import com.info.zhiduoduo.common.utils.NumberUtil;
import com.info.zhiduoduo.core.service.WxPartnerMessageService;
import com.info.zhiduoduo.repository.dto.partner.PartnerDetailDto;
import com.info.zhiduoduo.repository.dto.partner.PartnerSubDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.marketingcooperative.MarketingCooperativeRecord;
import com.info.zhiduoduo.repository.entity.partner.Partner;
import com.info.zhiduoduo.repository.entity.partner.PartnerAccount;
import com.info.zhiduoduo.repository.entity.partner.PartnerAuthRecord;
import com.info.zhiduoduo.repository.entity.partner.PartnerBrokerage;
import com.info.zhiduoduo.repository.entity.partner.PartnerCompany;
import com.info.zhiduoduo.repository.entity.partner.PartnerPerformanceNotice;
import com.info.zhiduoduo.repository.entity.partner.PartnerRecommendCompany;
import com.info.zhiduoduo.repository.entity.partner.PartnerVaccineRecord;
import com.info.zhiduoduo.repository.entity.partner.PartnerWithdraw;
import com.info.zhiduoduo.repository.entity.weixin.WxNotice;
import com.info.zhiduoduo.wxsdk.WxApiService;
import com.info.zhiduoduo.wxsdk.config.WxPartnerConfig;
import com.info.zhiduoduo.wxsdk.domain.app.WxTempleteMessage;

import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Service("wxPartnerMessageService")
public class WxPartnerMessageServiceImpl implements WxPartnerMessageService {

	@Autowired
	WxPartnerConfig wxPartnerConfig;
	@Autowired
	WxApiService wxApiService;
	@Autowired
	PayConfig payConfig;
	@Autowired
	CommonConfig commonConfig;
	@Autowired
	WhetherTestConfig whetherTestConfig;
	@Autowired
	NotificationConfig notificationConfig;

	/**
	 * 专门测试用的, 使用一个通用模板
	 */
	private static final String TEMP_TEST = "BlUjsCpze1iDbeeGwTCovrAdCBa9m7eHMlUkH42Hfcw";
	/**
	 * 回款到账通知
	 */
	private static final String TEMP_IN_ACCOUNT_CHECKED = "xqrvYOJ2QVW3Xzn2W8FoO14PJR0AUoS0mD6VkbYP5Z0";
	/**
	 * 业务状态变更通知
	 */
	private static final String TEMP_STATE_CHANGED = "ii9UqJT4fcwRqFREBmuKcxYhjgYqFkF9x_lB-3_pQaA";
	/**
	 * 认证审核通知
	 */
	private static final String TEMP_AUTH_NOTIFY = "wYae1FoED0OQjh0KgHUkFXcHFcBv3RSxW57gn5Ks2sE";
	/**
	 * 公告通知,业务处理结果通知
	 */
	private static final String TEMP_SEND_NOTICE = "Cl7102lPLgQUHFQAZHjJBO1l2CPL15flcwQyC6XDhrE";
	/**
	 * 注册成功通知
	 */
	private static final String TEMP_REGISTER_NOTIFY = "ju2AtyfpwtPdN1fQ7yrnoVrNiGd5BoJOwaliRS-3zOA";
	/**
	 * 问卷调查公告通知
	 */
	private static final String TEMP_SURVEY_NOTICE = "vKSV5smmI5h9zd4mxRyIVyu4FMqqCQJu1MyEwso7gZs";

	ExecutorService executor;

	public WxPartnerMessageServiceImpl() {
		this.executor = Executors.newSingleThreadExecutor();
	}

	/**
	 * 发送模板消息,如果直接在当前线程发送会堵塞当前线程,造成服务器与前端交互等待时间太长,所以这里采用线程executor,单独执行
	 *
	 * @param message
	 * @return
	 */
	private Future sendMessage(WxTempleteMessage message, String name, Integer type, String templateId, Integer userId) {
		if (type != null) {
			String first = "";
			String remark = "";
			String str = "";

			Map<String, WxTempleteMessage.ValueColor> data = message.getData();
			Iterator<Map.Entry<String, WxTempleteMessage.ValueColor>> iterator = data.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry<String, WxTempleteMessage.ValueColor> next = iterator.next();
				String key = next.getKey();
				if (key.equals("first")) {
					first = next.getValue().getValue();
				} else if (key.equals("remark")) {
					remark = next.getValue().getValue();
				} else {
					str += next.getValue().getValue();
				}
			}
			new WxNotice().setNoticeType(type)
					.setNoticePeople(name)
					.setNoticeTitle(first)
					.setNoticeRemarks(remark)
					.setNoticeContent(str)
					.setNoticeTemplate(templateId)
					.setUserId(userId)
					.insert();
		}
		return executor.submit(() -> {
			wxApiService.sendTempleteMessage(message, wxPartnerConfig.getAppid(), wxPartnerConfig.getSecret());
		});
	}

	@Override
	public boolean partnerAuthState(int acctid) {

		PartnerAccount partnerAccount = new PartnerAccount().selectById(acctid);
		if (partnerAccount == null) return false;

		Partner partner = new Partner().selectById(partnerAccount.getPartnerid());
		if (partner == null) return false;

		PartnerAuthRecord record = new PartnerAuthRecord().selectOne(
				Condition.create().eq("acctid", acctid).orderBy("id", false).last("limit 1"));

		String authResult;
		String remark = "";
		switch (record.getState()) {
			case 1:
				authResult = "审核失败,原因-" + record.getReason();
				remark = "请重新提交审核资料,谢谢";
				break;
			case 0:
				authResult = "审核通过!";
				remark = "关于平台的产品、服务、利润分享计划详情，可以马上咨询平台商务伙伴，预祝我们合作愉快！干杯！\n好友judy";
				break;
			default:
				authResult = "";

		}

		String openid = partner.getOpenid();
/*
		标题认证审核通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		申请人：{{keyword1.DATA}}
		审核结果：{{keyword2.DATA}}
		审核时间：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(openid);
		message.setTemplate_id(TEMP_AUTH_NOTIFY);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("合伙人实名认证审核通知", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of("合伙人-" + partnerAccount.getRealname()));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(authResult));
		data.put("keyword3", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
		data.put("remark", WxTempleteMessage.ValueColor.of(remark));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_AUTH_NOTIFY, partnerAccount.getPartnerid());
		return true;
	}

	@Override
	public boolean partnerRenew(int acctid) {

		PartnerAccount partnerAccount = new PartnerAccount().selectById(acctid);
		if (partnerAccount == null) return false;

		Partner partner = new Partner().selectById(partnerAccount.getPartnerid());
		if (partner == null) return false;

		String authResult;
		String remark = "";

		authResult = "合伙人续约成功!";
		remark = "关于平台的产品、服务、利润分享计划详情，可以马上咨询平台商务伙伴，预祝我们合作愉快！干杯！\n好友judy";

		String openid = partner.getOpenid();
/*
		标题认证审核通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		申请人：{{keyword1.DATA}}
		审核结果：{{keyword2.DATA}}
		审核时间：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(openid);
		message.setTemplate_id(TEMP_AUTH_NOTIFY);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("合伙人续约成功通知", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of("合伙人-" + partnerAccount.getRealname()));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(authResult));
		data.put("keyword3", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
		data.put("remark", WxTempleteMessage.ValueColor.of(remark));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_AUTH_NOTIFY, partnerAccount.getPartnerid());
		return true;
	}

	@Override
	public boolean noticeInvitePartner(String realname, String openid, Integer partnerid) {
/*
		标题注册成功通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		会员帐号：{{keyword1.DATA}}
		注册时间：{{keyword2.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(openid);
		message.setTemplate_id(TEMP_REGISTER_NOTIFY);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("您邀请的合伙人已成功注册", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of("合伙人-" + realname));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
		message.setData(data);

		sendMessage(message, realname, EmailType.PARTNER, TEMP_REGISTER_NOTIFY, partnerid);
		return true;
	}

	@Override
	public boolean noticeInviteCompany(String realname, String openid, Integer compId) {
/*
		标题注册成功通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		会员帐号：{{keyword1.DATA}}
		注册时间：{{keyword2.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(openid);
		message.setTemplate_id(TEMP_REGISTER_NOTIFY);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("您邀请的企业已成功注册", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of("企业-" + realname));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
		message.setData(data);

		sendMessage(message, realname, EmailType.COMP, TEMP_REGISTER_NOTIFY, compId);
		return true;
	}

	@Override
	public boolean notifyPartnerCompanyVerify(PartnerRecommendCompany recommendCompany) {
		Partner partner = new Partner().selectById(recommendCompany.getPartnerid());
		if (partner == null) return false;
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		Company company = new Company().selectById(recommendCompany.getActualCompanyid());
		if (company == null) return false;

		String authResult;
		String remark = "";
		switch (recommendCompany.getState()) {
			case Constants.PARTNER_RECOMMEND_REJECT:
				authResult = "审核失败,原因-" + recommendCompany.getReason();
				break;
			case Constants.PARTNER_RECOMMEND_HANDLED:
				authResult = "审核通过!";
				remark = "推荐客户成功，您真棒！加油加油！\n好友judy";
				break;
			default:
				authResult = "";

		}

		String openid = partner.getOpenid();
/*
		标题认证审核通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		申请人：{{keyword1.DATA}}
		审核结果：{{keyword2.DATA}}
		审核时间：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(openid);
		message.setTemplate_id(TEMP_AUTH_NOTIFY);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("合伙人推荐企业审核通知", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of("企业名-" + company.getCompname()));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(authResult));
		data.put("keyword3", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
		data.put("remark", WxTempleteMessage.ValueColor.of(remark));
		message.setData(data);
		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_AUTH_NOTIFY, partnerAccount.getPartnerid());
		return true;
	}

	@Override
	public boolean notifyPartnerCompanyAdded(PartnerCompany partnerCompany) {
		Partner partner = new Partner().selectById(partnerCompany.getPartnerid());
		if (partner == null) return false;
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		Company company = new Company().selectById(partnerCompany.getCompid());
		if (company == null) return false;

		String authResult = "审核通过!";
		String remark = "推荐客户成功，您真棒！加油加油！\n好友judy";

		String openid = partner.getOpenid();
/*
		标题认证审核通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		申请人：{{keyword1.DATA}}
		审核结果：{{keyword2.DATA}}
		审核时间：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(openid);
		message.setTemplate_id(TEMP_AUTH_NOTIFY);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("合伙人推荐企业审核通知", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of("企业名-" + company.getCompname()));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(authResult));
		data.put("keyword3", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
		data.put("remark", WxTempleteMessage.ValueColor.of(remark));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_AUTH_NOTIFY, partnerAccount.getPartnerid());
		return true;
	}

	@Override
	public boolean notifyPartnerRatioEdit(PartnerCompany partnerCompany) {
		Partner partner = new Partner().selectById(partnerCompany.getPartnerid());
		if (partner == null) return false;
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		Company company = new Company().selectById(partnerCompany.getCompid());
		if (company == null) return false;

		String openid = partner.getOpenid();
/*
		标题业务处理结果通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(openid);
		message.setTemplate_id(TEMP_SEND_NOTICE);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of(" 合伙人分佣比例设置成功", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of("企业名-" + company.getCompname()));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
		data.put("keyword3", WxTempleteMessage.ValueColor.of("分佣比例:" + NumberUtil.formatPercent(partnerCompany.getRatio(), 2)));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_SEND_NOTICE, partnerAccount.getPartnerid());
		return true;
	}

	@Override
	public boolean notifyPartnerBrokerageSettled(PartnerBrokerage brokerage, String projectname) {
		if (brokerage == null) return false;

		Partner partner = new Partner().selectById(brokerage.getPartnerid());
		if (partner == null) return false;
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		String openid = partner.getOpenid();
/*
		{{first.DATA}}

		详细内容{{first.DATA}}
		项目名称：{{keyword1.DATA}}
		到账金额：{{keyword2.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(openid);
		message.setTemplate_id(TEMP_IN_ACCOUNT_CHECKED);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("项目佣金到款通知", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of(brokerage.getBrokerageno()));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(brokerage.getBrokerage().setScale(2, RoundingMode.DOWN).toString()));
		data.put("remark", WxTempleteMessage.ValueColor.of("请注意查收,谢谢!"));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_IN_ACCOUNT_CHECKED, partnerAccount.getPartnerid());
		return true;
	}

	@Override
	public boolean notifyPartnerWithdrawSucceed(PartnerWithdraw withdraw) {
		Partner partner = new Partner().selectById(withdraw.getPartnerid());
		if (partner == null) return false;

		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", withdraw.getPartnerid()));
		if (partnerAccount == null) return false;
		String openid = partner.getOpenid();
/*
		{{first.DATA}}

		详细内容{{first.DATA}}
		项目名称：{{keyword1.DATA}}
		到账金额：{{keyword2.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(openid);
		message.setTemplate_id(TEMP_IN_ACCOUNT_CHECKED);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("合伙人提现到账通知", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of(withdraw.getWdno()));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(withdraw.getAmount().setScale(2, RoundingMode.DOWN).toString()));
		data.put("remark", WxTempleteMessage.ValueColor.of("请注意查收,谢谢!"));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_IN_ACCOUNT_CHECKED, partnerAccount.getPartnerid());
		return true;
	}

	@Override
	public boolean sendNotice(String title, String url, String openid) {

		/*
		标题业务处理结果通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(openid);
		message.setTemplate_id(TEMP_SEND_NOTICE);
		message.setUrl(url);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of(" 平台公告", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of(title));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
		data.put("keyword3", WxTempleteMessage.ValueColor.of("请点击查看详情"));
		message.setData(data);

		sendMessage(message, null, null, TEMP_SEND_NOTICE, null);
		return true;
	}

	@Override
	public boolean sendWelcomeNotice(PartnerDetailDto partner) {

		/*
		标题注册成功通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		会员帐号：{{keyword1.DATA}}
		注册时间：{{keyword2.DATA}}
		{{remark.DATA}}
		*/
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		String remark = "您好，目前对平台的产品和服务已经基本熟悉了吗？如果还不熟悉，可以随时咨询平台商务经理哦。" + "平台商务伙伴可以陪同拜访客户、和客户微信群在线沟通、组织线上线下商务沙龙等。预祝一切顺利！\n好友judy";

		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(partner.getOpenid());
		message.setTemplate_id(TEMP_REGISTER_NOTIFY);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of(" 平台公告", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of(partner.getRealname()));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(partner.getCreatetime())));
		data.put("remark", WxTempleteMessage.ValueColor.of(remark));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_REGISTER_NOTIFY, partnerAccount.getPartnerid());
		return true;
	}

	@Override
	public boolean sendPerformanceNotice(PartnerDetailDto partner, int type, int count) {
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		/*
		标题业务处理结果通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/

		String result = "";
		switch (type) {
			case 1: //上个月
				if (count <= 0) {
					result = "过去一个月，您成功推荐客户0个，虽然平时很忙碌，还是要继续努力啊！\n好友judy";
				} else {
					result = "过去一个月，您成功推荐" + count + "个客户，给您点赞！下个月继续加油！\n好友judy";
				}
				break;
			case 2: //过去2个月
				result = "过去两个月，您成功推荐客户0个，虽然平时很忙碌，还是要继续努力啊！\n好友judy";
				break;
			case 3: //过去3个月
				result = "过去一个季度，您成功推荐客户0个，judy真心有点小失望啊！\n好友judy";
				break;
			default:

				break;
		}

		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(partner.getOpenid());
		message.setTemplate_id(TEMP_SEND_NOTICE);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of(" 平台公告", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of(partner.getRealname()));
		// 2021-03-12 修改为 系统当前时间  partner.getEffectivedate()
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(new Date())));
		data.put("keyword3", WxTempleteMessage.ValueColor.of(result));
		data.put("remark", WxTempleteMessage.ValueColor.of(""));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_SEND_NOTICE, partnerAccount.getPartnerid());

		//保存记录
		new PartnerPerformanceNotice().setPartnerid(partner.getPartnerid())
				.setRealname(partner.getRealname())
				.setNotice(JSON.toJSONString(message))
				.insert();

		return true;
	}

	@Override
	public boolean sendSurveyNotice(String title, String content, String url, String openid) {
		/*

		模版ID：vKSV5smmI5h9zd4mxRyIVyu4FMqqCQJu1MyEwso7gZs
				开发者调用模版消息接口时需提供模版ID
		标题问卷接收成功通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		问卷主题：{{keyword1.DATA}}
		发起时间：{{keyword2.DATA}}
		结束时间：{{keyword3.DATA}}
		{{remark.DATA}}
		*/

		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(openid);
		message.setTemplate_id(TEMP_SURVEY_NOTICE);
		message.setUrl(url);

		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of(title, "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of(content));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(new Date())));
		data.put("keyword3", WxTempleteMessage.ValueColor.of("无"));
		data.put("remark", WxTempleteMessage.ValueColor.of(""));
		message.setData(data);

		sendMessage(message, null, null, TEMP_SURVEY_NOTICE, null);
		return true;
	}

	@Override
	public boolean noticeSubPartnerAdded(PartnerSubDto sub) {

		Partner partner = new Partner().selectById(sub.getPartnerid());
		if (partner == null) return false;
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		String rate = NumberUtil.formatDoubleRoundCeiling(sub.getRate() * 100, 2) + "%";
		/*
		标题注册成功通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		会员帐号：{{keyword1.DATA}}
		注册时间：{{keyword2.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(partner.getOpenid());
		message.setTemplate_id(TEMP_REGISTER_NOTIFY);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("二级合伙人绑定成功通知", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of("二级合伙人-" + sub.getSubPartnerName()));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
		data.put("remark", WxTempleteMessage.ValueColor.of("分佣比例：" + rate));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_REGISTER_NOTIFY, partnerAccount.getPartnerid());
		return true;
	}

	@Override
	public boolean notifyPartnerKpi(PartnerDetailDto partner, String kpiStr) {
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		/*
		标题业务处理结果通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(partner.getOpenid());
		message.setTemplate_id(TEMP_SEND_NOTICE);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of(" 平台公告", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of(partner.getRealname()));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(new Date())));
		data.put("keyword3", WxTempleteMessage.ValueColor.of(kpiStr));
		data.put("remark", WxTempleteMessage.ValueColor.of(""));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_SEND_NOTICE, partnerAccount.getPartnerid());

		//保存记录
		new PartnerPerformanceNotice().setPartnerid(partner.getPartnerid())
				.setRealname(partner.getRealname())
				.setNotice(JSON.toJSONString(message))
				.insert();

		return true;
	}

	@Override
	public boolean notifyPartnerRegisterStudio(int partnerid) {

		Partner partner = new Partner().selectById(partnerid);
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		/*
		标题业务处理结果通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(partner.getOpenid());
		message.setTemplate_id(TEMP_SEND_NOTICE);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("请注册创客工作室", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of("合伙人提现模式已设置为工作室模式"));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(new Date())));
		data.put("keyword3", WxTempleteMessage.ValueColor.of("合伙人必须绑定工作室才能提现，请尽快注册工作室并绑定。"));
		data.put("remark", WxTempleteMessage.ValueColor.of(""));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_SEND_NOTICE, partnerAccount.getPartnerid());

		return true;
	}

	@Override
	public boolean notifyPartnerApplyVaccineRecord(PartnerVaccineRecord record) {
		Partner partner = new Partner().selectById(record.getPartnerid());
		if (partner == null) return false;
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		/*
		标题业务处理结果通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/

		List<String> opendis = Lists.newArrayList();
		if (whetherTestConfig.isTest()) {
			opendis.addAll(notificationConfig.getInternalPartnerNoticeOpenids());
		} else {
			opendis.add(notificationConfig.getHuxiaofeng());
			opendis.addAll(notificationConfig.getInternalPartnerNoticeOpenids());
		}

		opendis.forEach(openid-> {
			WxTempleteMessage message = new WxTempleteMessage();
			message.setTouser(openid);
			message.setTemplate_id(TEMP_SEND_NOTICE);
			Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
			data.put("first", WxTempleteMessage.ValueColor.of("又有新订单咯", "#000000"));
			data.put("keyword1", WxTempleteMessage.ValueColor.of("客户需求"));
			data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(new Date())));
			data.put("keyword3", WxTempleteMessage.ValueColor.of(
					String.format("联系人：%s\n电话：%s\n品类：%s\n数量：%s\n请及时处理哦！", record.getContactName(), record.getContactPhone(),
							record.getVaccineBrand(), record.getVaccineAmount())));
			data.put("remark", WxTempleteMessage.ValueColor.of(""));
			message.setData(data);

			sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_SEND_NOTICE, partnerAccount.getPartnerid());
		});

		return true;
	}

	@Override
	public boolean notifyPartnerVaccineRecordDealed(PartnerVaccineRecord record) {
		Partner partner = new Partner().selectById(record.getPartnerid());
		if (partner == null) return false;
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		/*
		标题业务处理结果通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(partner.getOpenid());
		message.setTemplate_id(TEMP_SEND_NOTICE);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("订单已成交", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of("佣金到账"));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(new Date())));
		data.put("keyword3", WxTempleteMessage.ValueColor.of(record.getBrokerage().setScale(2, BigDecimal.ROUND_DOWN).toString() + "元"));
		data.put("remark", WxTempleteMessage.ValueColor.of(""));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_SEND_NOTICE, partnerAccount.getPartnerid());

		return true;
	}

	@Override
	public boolean notifyPartnerApplyMarketingCooperativeRecord(MarketingCooperativeRecord record) {
		Partner partner = new Partner().selectById(record.getPartnerid());
		if (partner == null) return false;
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		/*
		标题业务处理结果通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/

		List<String> opendis = Lists.newArrayList();
		if (whetherTestConfig.isTest()) {
			opendis.addAll(notificationConfig.getInternalPartnerNoticeOpenids());
		} else {
			// opendis.add(notificationConfig.getHuxiaofeng());
			opendis.addAll(notificationConfig.getInternalPartnerNoticeOpenids());
		}

		opendis.forEach(openid-> {
			WxTempleteMessage message = new WxTempleteMessage();
			message.setTouser(openid);
			message.setTemplate_id(TEMP_SEND_NOTICE);
			Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
			data.put("first", WxTempleteMessage.ValueColor.of("又有新订单咯", "#000000"));
			data.put("keyword1", WxTempleteMessage.ValueColor.of("客户需求"));
			data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(new Date())));
//			data.put("keyword3", WxTempleteMessage.ValueColor.of(
//					String.format("联系人：%s\n电话：%s\n品类：%s\n数量：%s\n请及时处理哦！", record.getContactName(), record.getContactPhone(),
//							record.getCommodityBrand(), record.getCommodityAmount())));
			data.put("remark", WxTempleteMessage.ValueColor.of(""));
			message.setData(data);

			sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_SEND_NOTICE, partnerAccount.getPartnerid());
		});

		return true;
	}

	@Override
	public boolean notifyMarketingCooperativeRecordDealed(MarketingCooperativeRecord record) {
		Partner partner = new Partner().selectById(record.getPartnerid());
		if (partner == null) return false;
		PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", partner.getPartnerid()));
		if (partnerAccount == null) return false;
		/*
		标题业务处理结果通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
		WxTempleteMessage message = new WxTempleteMessage();
		message.setTouser(partner.getOpenid());
		message.setTemplate_id(TEMP_SEND_NOTICE);
		Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
		data.put("first", WxTempleteMessage.ValueColor.of("订单已成交", "#000000"));
		data.put("keyword1", WxTempleteMessage.ValueColor.of("佣金到账"));
		data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(new Date())));
		data.put("keyword3", WxTempleteMessage.ValueColor.of(record.getBrokerage().setScale(2, BigDecimal.ROUND_DOWN).toString() + "元"));
		data.put("remark", WxTempleteMessage.ValueColor.of(""));
		message.setData(data);

		sendMessage(message, partnerAccount.getRealname(), EmailType.PARTNER, TEMP_SEND_NOTICE, partnerAccount.getPartnerid());

		return true;
	}
}
